<?php
class ExportsController extends AppController {

	var $name = 'Exports';
	var $helpers = array('Csv','Bibtex');
	var $uses = array('Item');

	// key   = dc_type_id
	// value = CSV ヘッダ
	var $arrCsvHeaders = array( // CSV ヘッダ表示用
		3	=>	'First Author',
		19	=>	'Date',
		25	=>	'ISBN',
		26	=>	'ISSN',
		30	=>	'URL',
		44	=>	'Publisher',
		49	=>	'Series',
		50	=>	'Series No.',
		75	=>	'Title',
		81	=>	'DOI',
		95	=>	'Publication',
		96	=>	'Volume',
		97	=>	'Number',
		98	=>	'Start Page',
		99	=>	'End Page',
	);

	// key   = dc_type_id
	// value = BibTeX ヘッダ
	var $arrBibTeXHeaders = array( // BibTeX ヘッダ表示用
		3	=>	'author', // 3 and 3
		19	=>	'year',
		26	=>	'ISSN',
		30	=>	'url',
		31	=>	'note',
		44	=>	'publisher',
		50	=>	'series',
		75	=>	'title',
		78	=>	'type',
		81	=>	'DOI',
		94	=>	'type',
		95	=>	'booktitle',
		96	=>	'volume',
		97	=>	'number',
		98	=>	'pages', // 98-99 前
		99	=>	'pages', // 98-99 後
	);

	// マッピング
	// key   = dc_type_id = 94 九大拡張 type.nii
	// value = BibTeX ヘッダ
	var $arrBibType = array( // BibTeX タイプ
		'Journal Article' => 'article' ,
		'Thesis or Dissertation' => 'phdthesis' ,
		'Departmental Bulletin Paper' => 'article' ,
		'Conference Paper' => 'inproceeding' ,
		'Presentation' => 'inproceeding' ,
		'Book' => 'book' ,
		'Technical Report' => 'techreport' ,
		'Research Paper' => 'techreport' ,
		'Article' => 'article' ,
		'Preprint' => 'unpublished' ,
		'Learning Material' => 'manual' ,
		'Data or database' => 'misc' ,
		'Software' => 'misc' ,
		'Others' => 'misc' ,
	);

	function beforeFilter(){
		// ファイル出力は重めな処理なので、リソース制限を無制限へ拡張
		ini_set('memory_limit', '-1');

		// ファイル出力なので不要な表示を制御する
		Configure::write('debug',0);
		$this->autoRender = false;
		$this->autoLayout = false;
	}

	function afterFilter() {
		// 「ini_set」はスクリプト終了時に元の値へ戻されるが明示的に戻す
		ini_restore('memory_limit');
	}

	function export() {
		if ( empty($this->data['LedaCollection']['id']) ) return false;
		if ( empty($this->params['form']) ) return false;

		switch ( key($this->params['form']) ) { // submitのnameを見て処理
			case 'csv' :
				$this->csv('csv');
				$this->render('csv');
				break;
			case 'utf' :
				$this->csv('utf');
				$this->render('csv');
				break;
			case 'bibtex' :
				$this->bibtex();
				$this->render('bibtex');
				break;
			default :
				return false;
		}
	}

	/**
	 * CSV出力
	 *
	 * @param (string) $textformat 文字コード変換するかしないか // @FIXME　mb_convert_variables の引数を入れれば任意の文字列へ変更できる
	 * @return void
	 */
	function csv($textformat) {
		$arrWhereIn = array();
		foreach($this->data['LedaCollection']['id'] AS $id=>$value) {
			if ($value) {
				$arrWhereIn[] = $id;
			}
		}

		if ( empty($arrWhereIn) ) {
			$this->set('Item', array());
			return true;
		}

		$query['conditions'] = array("Item.item_id" => $arrWhereIn );

		$this->Item->recursive = 2;
		$this->Item->unbindModel( array( 'hasMany' => array( 'Itemsbyauthor' ) ) );
		$this->Item->unbindModel( array( 'hasOne' => array( 'Itemsbydate','Itemsbytitle' ) ) );
		$items = $this->Item->find('all',$query);
		// 出力文字コードの変換
		if ($textformat == 'csv') {
			mb_convert_variables('SJIS-win', 'UTF-8', $items);
		}

		$this->set('Headers', $this->arrCsvHeaders);
		$this->set('Items', $items);

		$today = date('Ymd');
		header("Content-type: application/octet-stream; name=profilepage.{$today}.csv");
		header("Content-Disposition: attachment; filename=profilepage.{$today}.csv");
	}

	/**
	 * BibTeX出力
	 *
	 * @return void
	 */
	function bibtex() {
		foreach($this->data['LedaCollection']['id'] AS $id=>$value) {
			if ($value) {
				$arrWhereIn[] = $id;
			}
		}

		if ( empty($arrWhereIn) ) {
			$this->set('Item', array());
			return true;
		}

		$query['conditions'] = array("Item.item_id" => $arrWhereIn );

		$this->Item->recursive = 2;
		$this->Item->unbindModel( array( 'hasMany' => array( 'Itemsbyauthor' ) ) );
		$this->Item->unbindModel( array( 'hasOne' => array( 'Itemsbydate','Itemsbytitle' ) ) );
		$items = $this->Item->find('all',$query);

		$this->set('Headers', $this->arrBibTeXHeaders);
		$this->set('BibTypes', $this->arrBibType);
		$this->set('Items', $items);

		$today = date('Ymd');
		header("Content-type: application/octet-stream; name=profilepage.{$today}.bib");
		header("Content-Disposition: attachment; filename=profilepage.{$today}.bib");
	}
}
?>